const { STRING } = require('sequelize')
const { db } = require('../core/sequelize')
const { Sequelize, Model, INTEGER, Op } = require('sequelize')
const { User } = require('./user')
// const { Posts } = require('./posts')
class Comments extends Model {
    static async delComments(id) {
        const res = await Comments.destroy({
            where: {
                id: id
            }
        })
        return res
    }
    static async createComment(data) {
        const res = await Comments.create(data)
        return res
    }

    static async getList(postid) {
        let commentList = []
        commentList = await Comments.findAll(
            {
                where: {
                    postid
                },
                // 关联查询
                include: {
                    model: User
                },
                order: [['createTime', 'DESC']],

            }
        )
        return commentList
    }
}
Comments.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true, //设置主键
            autoIncrement: true,
        },
        userid: Sequelize.STRING, //类型 字符串
        postid: Sequelize.STRING, //类型 字符串
        content: Sequelize.STRING, //类型 字符串
        createTime: { type: STRING, field: 'createTime' },
        imgs: Sequelize.STRING, //类型 字符串
    },
    {
        // underscored: true,
        sequelize: db,
        modelName: 'comments', timestamps: false
    },

)
// 关联
Comments.belongsTo(User, {
    foreignKey: 'userid',
    sourceKey: 'id'
});
// Comments.hasMany(Posts);
module.exports = { Comments }
